package com.yonyou.model;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Map;

/**
 * Created by Administrator on 2018-06-14.
 * 返回报文实体类，用于ajax统一异常处理
 * @Author jyb
 */
@Data
@Accessors(chain = true)
public class ResultEntity<T> {
    /**
     * error_code 状态值：0 极为成功，其他数值代表失败
     */
    private Integer code;

    /**
     * error_message 错误信息，若code为0时，为success
     */
    private String message;

    /**
     * content 返回体报文的出参，使用泛型兼容不同的类型
     */
    private T data;


    public ResultEntity(String message, Integer code) {
        this.message = message;
        this.code = code;
    }

    public ResultEntity() {

    }

    public ResultEntity(T data, String message, Integer code) {

        this.data = data;
        this.message = message;
        this.code = code;
    }

    public static ResultEntity<?> exception(Integer code, String message){
        return new ResultEntity(message,code);
    }

    public static ResultEntity<?> exception(String message){
        return new ResultEntity(message, 500);
    }

    public static ResultEntity<?> success(Integer code,String message){
        return new ResultEntity(message,code);
    }

    public static ResultEntity<?> exception(Integer code,String message,Map<String, String> map){
        return new ResultEntity<>(map,message,code);
    }

    public static ResultEntity<?> success(Integer code,String message,Map<String, String> map){
        return new ResultEntity<>(map,message,code);
    }

    public static ResultEntity<?> success(Integer code,String message,Object t){
        return new ResultEntity<>(t,message,code);
    }

    public static ResultEntity<?> success(Object t){
        return new ResultEntity<>(t,"", 200);
    }

    public static ResultEntity<?> success(){
        return new ResultEntity<>("", 200);
    }

    public static ResultEntity<?> success(String message){
        return new ResultEntity<>(message, 200);
    }

    public static ResultEntity<?> success(String message, Object t){
        return new ResultEntity<>(t, message, 200);
    }

    public static ResultEntity<?> fail(Integer code, String message){
        return new ResultEntity<>(message, code);
    }

    public static ResultEntity<?> fail(String message){
        return new ResultEntity<>(message, 500);
    }

    public static ResultEntity<?> fail(){
        return new ResultEntity<>("", 500);
    }
}